<template>
	<view class="header-bg-image header-bg">
		<view style="height: 50rpx;"></view>
		<view class="padding-30">
			<view class="margin-30-0 fontSize-18 color-000 flex1 text-center">资讯</view>
			<view class="margin-30-0">
				<u-tabs :list="list1" lineColor="#967C5E" @click="chooseTabs"></u-tabs>
			</view>
			<!-- list列表 -->
			<view class="padding-30 flex-between border-radius20 bg-fff margin-bottom30"
			@click="$tools.navigateTo('/pages/news/news-info?article_id='+item.id)" v-for="(item,index) in list" :key="index">
				<view class="">
					<view class="fontSize-15 color-000 margin-bottom30">{{item.title}}</view>
					<view class="flex-alignItems-center">
						<u-icon name="share-square" color="#967C5E" size="25"></u-icon>
						<view class="fontSize-13 color-999 margin-left20">{{item.share_count}}</view>
					</view>
				</view>
				<image class="img-160 margin-left20" :src="item.image" mode=""></image>
			</view>
						
			<u-empty v-if="list.length == 0"
			        mode="list"
			        icon="http://cdn.uviewui.com/uview/empty/list.png"
					width="150px"
					height="150px"
					text="列表为空"
					textSize="18"
			>
			</u-empty>
			<view style="padding-bottom: 100rpx;">
				<u-loadmore v-if="loading" status="loading" />
				<u-loadmore v-if="isLastPage" status="nomore" />
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list1: [],
				page:1,
				limit:10,
				cate_id:'', //分类id(可选)
				is_top:1, //0不推荐1推荐(可选)
				list:[],
				isLastPage: false,
				loading: false,
			}
		},
		onShow() {
			this.page = 1
			this.limit = 10
			this.list = []
			this.getCateList()
			this.getArticleList()
		},
		// 刷新 上拉刷新
		onPullDownRefresh() {
			this.page = 1
			this.isLastPage = false
			this.list = []
			this.getArticleList().finally(() => {
				// 停止当前页面下拉刷新
				uni.stopPullDownRefresh();
			});
		},
		// 加载下一页 下拉加载
		onReachBottom() {
			if (this.list.length >= this.page * this.limit) {
				this.isLastPage = false;
				this.page += 1;
				this.getArticleList();
			} else {
				this.isLastPage = true
			}
		},
		methods: {
			// 获取文章列表
			async getArticleList(){
				let form = {
					page:this.page,
					limit:this.limit,
					cate_id:this.cate_id,
					is_top:this.is_top
				}
				this.$http.post(this.$api.article_list,form,{token:true}).then(res => {
					this.list = [...this.list,...res.data]
				}).finally(() => {
					// 停止当前页面下拉刷新
					uni.stopPullDownRefresh();
				})
			},
			// 选择分类
			chooseTabs(e){
				if(e.name == '推荐'){
					this.cate_id= ''
					this.is_top = 1
				}else{
					this.cate_id = e.id
					this.is_top = ''
				}
				this.page = 1
				this.limit = 10
				this.list = []
				this.getArticleList()
			},
			// 获取分类
			async getCateList(){
				let res = await this.$http.post(this.$api.cate_list,{
					page:1,
					limit:100,
					type:3
				},{token:true})
				if(res.code ==1){
					this.list1 = res.data.map(item=>{
						return {
							id: item.id,
							name:item.name
						}
					})
				}
				this.list1.unshift({id:'1231',name:'推荐'})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header-bg-image {
		width: 100%;
		min-height: 100vh;
	}
</style>